package model

import (
	"database/sql"
	pb "git.huoys.com/block_chain/mall/internal/shop/api"
)

type Order struct {
	ID               uint64         `json:"id" gorm:"primaryKey;column:id;type:bigint auto_increment"`
	UID              uint64         `json:"uid" gorm:"column:uid;type:bigint;not null;index:order_uid_key;comment:用户id"`                                                   // 用户id
	GoodsID          uint64         `json:"goods_id" gorm:"column:goods_id;type:bigint;not null;comment:商品id"`                                                             // 商品id
	GoodsAmountTotal float32        `json:"goods_amount_total" gorm:"column:goods_amount_total;type:decimal(12,4);not null;comment:商品总价"`                                  // 商品总价
	Status           pb.OrderStatus `json:"status" gorm:"column:status;type:tinyint;not null;default:1;comment:订单状态 1未知,2未付款,3已付款,4已发货,5已签收,6交易成功,7退货申请,8退货中,9已退货,10取消交易"` // 订单状态 1未知,2未付款,3已付款,4已发货,5已签收,6交易成功,7退货申请,8退货中,9已退货,10取消交易
	PayTotal         float32        `json:"pay_total" gorm:"column:pay_total;type:decimal(12,4);not null;comment:实付价格"`                                                    // 实付价格
	PayTime          int64          `json:"pay_time" gorm:"column:pay_time;type:int;not null;default:0;comment:付款时间"`                                                      // 付款时间
	BlockHash        string         `json:"block_hash" gorm:"column:block_hash;type:varchar(128);not null;default '';comment:区块交易标识"`                                      // 区块交易标识
	CreatedAt        sql.NullTime   `json:"created_at" gorm:"column:created_at;type:timestamp"`
	UpdatedAt        sql.NullTime   `json:"updated_at" gorm:"column:updated_at;type:timestamp"`
}

// TableName returns the table name of the Order model
func (o *Order) TableName() string {
	return "order"
}

func PbOrderToOrder(req pb.AddOrderReq) (resp Order) {
	resp = Order{
		UID:     req.GetUid(),
		GoodsID: req.GetGoodsId(),
	}
	return resp
}

func (o Order) ToPbOrder() (resp *pb.Order) {
	resp = &pb.Order{
		Id:          o.ID,
		Uid:         o.UID,
		OrderId:     o.GoodsID,
		AmountTotal: o.GoodsAmountTotal,
		Status:      o.Status,
		PayTotal:    o.PayTotal,
		PayTime:     uint64(o.PayTime),
		BlockHash:   o.BlockHash,
	}
	return resp
}
